package com.bm.greeting.config;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSONArray;
import com.bm.greeting.entity.Weather;

import java.util.HashMap;
import java.util.Map;

public class SendWxMessageUtil {

    //新建应用的agentid
    private static String agentid = "1000002";
    // 新建应用的corpsecret
    private static String corpsecret = "x2j2Z2ALA6s9zvUFJyI6NUZ-Roskys5XaRrBYSnUqSg";
    //企业ID
    private static String corpid = "ww23033ffbd6362658";

    //腾讯企业微信接口：获取TOKEN地址(GET)
    private static String apiGetTokenUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + corpid + "&corpsecret=" + corpsecret;
    //腾讯企业微信接口：发送消息(POST)
    private static String apiSendMessageUrl = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=";
    private static String qun = "https://qyapi.weixin.qq.com/cgi-bin/appchat/create??access_token=";

    /***
     * 发送企业微信文本类消息* @param userId   用户ID可在企业微信后台查看关注用户的ID，@all表示全体用户* @param content  消息正文
     * */
    public static String sendTextMsg() {
        HttpRequest httpRequest = HttpUtil.createGet(apiGetTokenUrl);
        String result = httpRequest.execute().body();
        cn.hutool.json.JSONObject data = JSONUtil.parseObj(result);
        System.out.println(data.get("access_token"));
        return data.get("access_token").toString();
    }

    /*** 发送企业微信文本类消息* @param userId   用户ID可在企业微信后台查看关注用户的ID，@all表示全体用户* @param content  消息正文*/
    public static void sendTextMsgData(String userId, String content, String token) {
        Map<String, Object> struct = new HashMap<>();
        //要发送的消息结构体
        Map<String, Object> msgcontent = new HashMap<>();
        //消息正文
        msgcontent.put("content", content);
        struct.put("agentid", agentid);
        //企业微信中的小程序ID
        struct.put("msgtype", "text");
//        struct.put("msgtype","markdown");
        //消息类型，这里是text文本型
        struct.put("touser", userId);
        //接收用户的ID，全部用户是"@all" 指定接收消息的成员，成员ID列表（多个接收者用‘|’分隔，最多支持1000个）
        struct.put("text", msgcontent);
//        struct.put("markdown",msgcontent);
        HttpRequest httpRequest = HttpUtil.createPost(apiSendMessageUrl + token)
                .body(JSONObject.toJSONString(struct));
        String result = httpRequest.execute().body();
        System.out.println(result);
    }

    public static String test() {
        Weather weather = ToolsUtils.getWeather();
        Map<String, String> yiTyjmap = ToolsUtils.getEnsentence();
        Map<String, String> qingShiMap = ToolsUtils.getQingShi();
        String data = "早上好, 我的小可爱\n";
        data += "今天是：" + weather.getDate() + " - " + weather.getWeek();
        data += "\n";
        data += "天气：" + weather.getText_now();
        data += "\n";
        data += "当前温度:" + weather.getTemp() + "℃";
        data += "\n";
        data += "最低温度:" + weather.getLow() + "℃";
        data += "\n";
        data += "最高温度:" + weather.getHigh() + "℃";
        data += "\n";
        data += "风级:" + weather.getWind_class();
        data += "\n";
        data += "风向:" + weather.getWind_dir();

        data += "\n";
//        data += "" + ToolsUtils.getCaiHongPi() ;
        Map map = ToolsUtils.getLaoHuangLi();
        data += "------------------------------------------------";
        data += "\n";
        data += "老黄历：";
        data += "\n";
        data += "公历日期：" + map.get("gregoriandate");
        data += "\n";
        if (null != map.get("festival") && !"".equals(map.get("festival"))) {
            data += "公历节日：" + map.get("festival");
            data += "\n";
        }
        data += "农历日期：" + map.get("lunardate");
        data += "\n";
        if (null != map.get("lunar_festival") && !"".equals(map.get("lunar_festival"))) {
            data += "农历节日：" + map.get("lunar_festival");
            data += "\n";
        }
        data += "今日适宜：" + map.get("fitness");
        data += "\n";
        data += "今日不宜：" + map.get("taboo");
        data += "\n";
        data += "神位：" + map.get("shenwei");
        data += "\n";
        data += "------------------------------------------------";
        data += "\n";
        data += "今天是我们恋爱的第" + JiNianRiUtils.getLianAi() + "天 ";

        data += "\n";
        data += "你老公大人的生日倒计时提醒：" + JiNianRiUtils.getBirthday_Jimmy() + "天 ";

        data += "\n";
        data += "小可爱的生日倒计时提醒：" + JiNianRiUtils.getBirthday_xy() + "天 ";
        data += "\n";
        data += "------------------------------------------------";
        data += "\n";
        data += "今日水瓶座运势：\n";
        data += "\n";
        JSONArray jsonArray = ToolsUtils.getStar();

        for (int i = 0, j = jsonArray.size(); i < j; i++) {
            data += jsonArray.getJSONObject(i).get("type") + ":" + jsonArray.getJSONObject(i).get("content");
            data += "\n";
        }
        data += "------------------------------------------------";


        data += "\n";
        data += "每日一句：\n" + yiTyjmap.get("en");
        data += "\n";
        data += "" + yiTyjmap.get("zh");
//        data += "\n";
//        data += "" + ToolsUtils.getTheTrickOfLife() ;

        data += "\n";
        data += "\n";
        data += "" + qingShiMap.get("content");
        data += "\n" + "<<" + qingShiMap.get("source") + ">> " + qingShiMap.get("author");
        data += "\n\n";
        data += "" + ToolsUtils.getGoodMorning();
        return data;
    }


}
